import 'package:get/get.dart';
import 'package:logger/logger.dart';
import 'package:tencent_cloud_chat_sdk/enum/V2TimConversationListener.dart';
import 'package:tencent_cloud_chat_sdk/enum/V2TimGroupListener.dart';
import 'package:tencent_cloud_chat_sdk/enum/message_elem_type.dart';
import 'package:tencent_cloud_chat_sdk/manager/v2_tim_conversation_manager.dart';
import 'package:tencent_cloud_chat_sdk/manager/v2_tim_manager.dart';
import 'package:tencent_cloud_chat_sdk/models/v2_tim_conversation_result.dart';
import 'package:tencent_cloud_chat_sdk/models/v2_tim_message.dart';
import 'package:tencent_cloud_chat_sdk/models/v2_tim_value_callback.dart';
import 'package:tencent_cloud_chat_sdk/tencent_im_sdk_plugin.dart';

/// 腾讯IM会话处理
class ConversationUtil {
  ConversationUtil._();
  static final Logger _logger = Get.find<Logger>();

  // 会话管理器
  static final V2TIMConversationManager _conversationManager =
      TencentImSDKPlugin.v2TIMManager.getConversationManager();
  static final V2TIMManager _v2timManager = TencentImSDKPlugin.v2TIMManager;

  /// 获取会话列表（一般情况下不会主动调用）
  static Future<V2TimConversationResult?> getConversationList({
    int count = 100,
    String nextSeq = "0",
  }) async {
    try {
      //获取会话列表
      V2TimValueCallback<V2TimConversationResult> res =
          await _conversationManager.getConversationList(
            count: count, //分页拉取的个数，一次分页拉取不宜太多，会影响拉取的速度，建议每次拉取 100 个会话
            nextSeq: nextSeq, //分页拉取的游标，第一次默认取传 0，后续分页拉传上一次分页拉取成功回调里的 nextSeq
          );

      if (res.code == 0) {
        //拉取成功
        return res.data;
      } else {
        _logger.e("获取会话列表失败: ${res.code} - ${res.desc}");
        return null;
      }
    } catch (e) {
      _logger.e("获取会话列表异常: $e");
      return null;
    }
  }

  // 设置会话置顶
  static Future<bool> pinConversation({
    required String conversationID,
    required bool isPinned,
  }) async {
    try {
      final res = await _conversationManager.pinConversation(
        conversationID: conversationID,
        isPinned: isPinned,
      );
      return res.code == 0;
    } catch (e) {
      _logger.e("置顶会话失败: $e");
      return false;
    }
  }

  // 删除会话
  static Future<bool> deleteConversation({required String conversationID}) async {
    try {
      final res = await _conversationManager.deleteConversation(
        conversationID: conversationID,
      );
      return res.code == 0;
    } catch (e) {
      _logger.e("删除会话失败: $e");
      return false;
    }
  }

  /// 添加会话监听
  static Future<void> addConversationListener(
    V2TimConversationListener listener,
  ) async {
    await _conversationManager.addConversationListener(listener: listener);
  }

  /// 移除会话监听
  static Future<void> removeConversationListener(
    V2TimConversationListener listener,
  ) async {
    await _conversationManager.removeConversationListener(listener: listener);
  }

  /// 添加群组会话监听
  static Future<void> addGroupListener(V2TimGroupListener listener) async {
    await _v2timManager.addGroupListener(listener: listener);
  }

  /// 移除群组会话监听
  static Future<void> removeGroupListener(V2TimGroupListener listener) async {
    await _v2timManager.removeGroupListener(listener: listener);
  }

  static String? getLastMessage(V2TimMessage? message) {
    switch (message?.elemType) {
      case MessageElemType.V2TIM_ELEM_TYPE_TEXT:
        return message?.textElem?.text;
      case MessageElemType.V2TIM_ELEM_TYPE_IMAGE:
        return '[${'messageType.image'.tr}]';
      case MessageElemType.V2TIM_ELEM_TYPE_SOUND:
        return '[${'messageType.sound'.tr}]';
      case MessageElemType.V2TIM_ELEM_TYPE_VIDEO:
        return '[${'messageType.video'.tr}]';
      case MessageElemType.V2TIM_ELEM_TYPE_FILE:
        return '[${'messageType.file'.tr}]';
      case MessageElemType.V2TIM_ELEM_TYPE_LOCATION:
        return '[${'messageType.location'.tr}]';
      case MessageElemType.V2TIM_ELEM_TYPE_FACE:
        return message?.faceElem?.data;
      case MessageElemType.V2TIM_ELEM_TYPE_MERGER:
        return '[${'messageType.merger'.tr}]';
      default:
        return '[${'messageType.notSupported'.tr}]';
    }
  }
}
